﻿using System;
using System.Configuration;

// METHOD 1
//using PostmarkDotNet;

// METHOD 2
using System.Net.Mail;

// METHOD 3
//using System.Web.Helpers;

// METHOD 4 (you will need to add a reference)
//using Microsoft.Exchange.WebServices.Data;


namespace Yelper.Web.Helpers {

    /**
     * We have included several ways to send email in this helper,
     * you can simply uncomment the one you would like to use.
     * 
     *  - Postmark
     *  - SMTP (2 methods)
     *  - Exchange
     * 
     **/

    public class EmailHelper {

        // METHOD 1: Send an email using Postmark (http://postmarkapp.com/)
        
        //public bool SendEmail(string to, string subject, string body) {

        //    // get the API key from Web.config
        //    string apikey = ConfigurationManager.AppSettings["Postmark"];
        //    // Example: add <add key="Postmark" value="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" />
        //    //          to the <appSettings> section of Web.config

        //    PostmarkMessage message = new PostmarkMessage {
        //        From = "noreply@yelper.eu",
        //        To = to,
        //        Subject = subject,
        //        TextBody = body,
        //        ReplyTo = "noreply@yelper.eu"
        //    };
        //    PostmarkClient client = new PostmarkClient(apikey);
        //    PostmarkResponse response = client.SendMessage(message);
        //    return response.Status == PostmarkStatus.Success;

        //}


        // METHOD 2: Send an email over good old SMTP

        public bool SendEmail(string to, string subject, string body) {

            // get the hostname of the SMTP server from Web.config
            string hostname = ConfigurationManager.AppSettings["SMTP"];
            // Example: add <add key="SMTP" value="mail-out.hostbasket.com" />
            //          to the <appSettings> section of Web.config
            string user = ConfigurationManager.AppSettings["SMTP_user"];
            string pwd = ConfigurationManager.AppSettings["SMTP_pwd"];

            MailMessage mail = new MailMessage();
            mail.From = new MailAddress("noreply@yelper.eu", "Yelper");
            mail.To.Add(to);
            mail.Subject = subject;
            mail.Body = body;
            mail.IsBodyHtml = false;
            SmtpClient client = new SmtpClient(hostname);
            client.Credentials = new System.Net.NetworkCredential(user,pwd);
            try {
                client.Send(mail);
                return true;
            } catch (Exception) {
                return false;
            }

        }

        // METHOD 3: Send an email over good old SMTP using an MVC 3 helper (works only in ASP.NET MVC 3)

        //public bool SendEmail(string to, string subject, string body) {

        //    // get the hostname of the SMTP server from Web.config
        //    string hostname = ConfigurationManager.AppSettings["SMTP"];
        //    // Example: add <add key="SMTP" value="mail-out.hostbasket.com" />
        //    //          to the <appSettings> section of Web.config

        //    WebMail.SmtpServer = hostname;
        //    WebMail.Send(to, subject, body, from: "noreply@yelper.eu");

        //    return true; // there's no way to tell if this worked

        //}


        // METHOD 4: Send an email using EWS (Exchange Web Services)
        // http://www.microsoft.com/online/exchange-online.aspx
        // http://msdn.microsoft.com/en-us/library/dd877037(v=exchg.140).aspx

        //public bool SendEmail(string to, string subject, string body) {

        //    // get the username and password from Web.config
        //    string username = ConfigurationManager.AppSettings["ExchangeUsername"];
        //    string password = ConfigurationManager.AppSettings["ExchangePassword"];

        //    var service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
        //    // pick the right server. this is north america
        //    service.Url = new Uri("https://red001.mail.microsoftonline.com/ews/exchange.asmx"); 
        //    service.Credentials = new WebCredentials(username, password);

        //    var message = new EmailMessage(service);
        //    message.ToRecipients.Add(to);
        //    message.Subject = subject;
        //    message.Body = new MessageBody(BodyType.Text, body);

        //    message.Send(); // or message.SendAndSaveCopy(); if you want to keep a copy in sent items

        //    return true;

        //}
 

    }

}